import 'package:flutter/foundation.dart';
import '../models/device.dart';
import '../models/user.dart';
import '../services/device_service.dart';

class DeviceProvider with ChangeNotifier {
  final DeviceService _deviceService = DeviceService();
  List<Device> _devices = [];
  bool _isLoading = false;
  String? _error;

  List<Device> get devices => [..._devices];
  bool get isLoading => _isLoading;
  String? get error => _error;

  // 获取设备列表
  Future<void> fetchDevices(User user) async {
    try {
      _isLoading = true;
      _error = null;
      notifyListeners();

      _devices = await _deviceService.getDevices(user);
      
      _isLoading = false;
      notifyListeners();
    } catch (e) {
      _isLoading = false;
      _error = e.toString();
      notifyListeners();
    }
  }

  // 获取单个设备详情
  Future<Device?> fetchDevice(User user, String deviceId) async {
    try {
      _error = null;
      notifyListeners();

      final device = await _deviceService.getDevice(user, deviceId);
      
      // 更新设备列表中的设备信息
      final index = _devices.indexWhere((d) => d.id == deviceId);
      if (index >= 0) {
        _devices[index] = device;
        notifyListeners();
      }

      return device;
    } catch (e) {
      _error = e.toString();
      notifyListeners();
      return null;
    }
  }

  // 添加新设备
  Future<Device?> addDevice(User user, Map<String, dynamic> deviceData) async {
    try {
      _isLoading = true;
      _error = null;
      notifyListeners();

      final device = await _deviceService.addDevice(user, deviceData);
      _devices.add(device);
      
      _isLoading = false;
      notifyListeners();
      return device;
    } catch (e) {
      _isLoading = false;
      _error = e.toString();
      notifyListeners();
      return null;
    }
  }

  // 更新设备状态
  Future<bool> toggleDeviceStatus(User user, String deviceId, bool status) async {
    try {
      _error = null;
      notifyListeners();

      final updatedDevice = await _deviceService.updateDeviceStatus(
        user,
        deviceId,
        status,
      );
      
      final index = _devices.indexWhere((d) => d.id == deviceId);
      if (index >= 0) {
        _devices[index] = updatedDevice;
        notifyListeners();
      }

      return true;
    } catch (e) {
      _error = e.toString();
      notifyListeners();
      return false;
    }
  }

  // 更新设备属性
  Future<bool> updateDeviceAttributes(
    User user,
    String deviceId,
    Map<String, dynamic> attributes,
  ) async {
    try {
      _error = null;
      notifyListeners();

      final updatedDevice = await _deviceService.updateDeviceAttributes(
        user,
        deviceId,
        attributes,
      );
      
      final index = _devices.indexWhere((d) => d.id == deviceId);
      if (index >= 0) {
        _devices[index] = updatedDevice;
        notifyListeners();
      }

      return true;
    } catch (e) {
      _error = e.toString();
      notifyListeners();
      return false;
    }
  }

  // 删除设备
  Future<bool> deleteDevice(User user, String deviceId) async {
    try {
      _isLoading = true;
      _error = null;
      notifyListeners();

      await _deviceService.deleteDevice(user, deviceId);
      _devices.removeWhere((device) => device.id == deviceId);
      
      _isLoading = false;
      notifyListeners();
      return true;
    } catch (e) {
      _isLoading = false;
      _error = e.toString();
      notifyListeners();
      return false;
    }
  }

  // 按类型筛选设备
  List<Device> filterByType(String type) {
    return _devices.where((device) => device.type == type).toList();
  }

  // 按状态筛选设备
  List<Device> filterByStatus(bool status) {
    return _devices.where((device) => device.status == status).toList();
  }

  // 清除错误信息
  void clearError() {
    _error = null;
    notifyListeners();
  }
}